package com.android.bonn.fast.widget.banner;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import com.android.bonn.fast.widget.R;


/**
 * author huangyunlin@snqu.com
 * date 2018/4/24
 * description
 * 使用参考 https://github.com/siralam/LoopingViewPager and https://github.com/LinweiJ/ViewPagerIndicator
 */
public class BannerView extends RelativeLayout {

    private LoopingViewPager mLoopingViewPager;
    private ViewPagerIndicator mViewPagerIndicator;

    public BannerView(Context context) {
        super(context);
        init();
    }

    public BannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BannerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.banner_view, this, true);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mLoopingViewPager = findViewById(R.id.banner);
        mViewPagerIndicator = findViewById(R.id.indicator);
    }

    public void setAdapter(LoopingPagerAdapter adapter) {
        mLoopingViewPager.setAdapter(adapter);
        mViewPagerIndicator.setViewPager(mLoopingViewPager, true);
    }

    public void resumeAutoScroll() {
        mLoopingViewPager.resumeAutoScroll();
    }

    public void pauseAutoScroll() {
        mLoopingViewPager.pauseAutoScroll();
    }
}
